using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;

namespace SqlRunner.Readers
{
	/// <summary>
	/// Summary description for CSVReader.
	/// </summary>
	public class CsvReader : IFolderReader
	{
		public CsvReader()
		{
			//
			// TODO: Add constructor logic here
			//
		}

        public List<string> ReadFolders(DragEventArgs e)
		{
			Trace.WriteLine("CSVReader - Reading dragged object");
			List<string> folders = new List<string>();
			// Check the type is a CSV stream
			if (e.Data.GetDataPresent(DataFormats.CommaSeparatedValue))
			{
				StreamReader reader = new StreamReader((MemoryStream) e.Data.GetData(DataFormats.CommaSeparatedValue));
				string path = reader.ReadLine();
				while (path != null)
				{
					try
					{
						DirectoryInfo aDir = new DirectoryInfo(path);
						if (aDir.Exists) 
							folders.Add(aDir.FullName);
						else
							Trace.WriteLine(String.Format("{0} could not be found", path));		
					}
					catch(Exception exc)
					{
						Trace.WriteLine(String.Format("Error when reading CSV data: {0}", exc.ToString()));
					}
					path = reader.ReadLine();
				}
				reader.Close();
			}
			return folders;
		}
	}
}
